clear all
clc

data = readtable('../../data/working/Sample.csv');
c = readtable('../../data/working/table5_col4.csv');

% sort in ascending order of household_id and then currency_id
data = sortrows(data,[2 3]);

unique_curr = unique(data.currency_id);

for ii=1:length(unique_curr)
    
    temp = unique_curr(ii);
    
    for nn=1:size(data,1)
        
        temp_ind = isequal(temp,data.currency_id(nn));
        
        if temp_ind==1
            
            data.currency_id2(nn) = ii;
            
        end
        
    end
    %ii
end

% data.open = data.open*10^3;

data.supply = data.marketcap./(data.open);

ID_unique = unique(data.household_id);

unique_curr = unique(data.currency_id);

% for ii=1:size(data,1)
%
%     data.first_time2(ii) = isequal(data.first_time(ii),{'First Purchase: Late'});
%
% end

for ii=1:length(unique_curr)
    
    temp = unique_curr(ii);
    
    for nn=1:size(data,1)
        
        temp_ind = isequal(temp,data.currency_id(nn));
        
        if temp_ind==1
            
            data.currency_id2(nn) = ii;
            
        end
        
    end
    %ii
end

data.pow = ones(size(data,1),1);

for ii=[1 8]

    temp_ind = (data.currency_id2==ii);
   
    data.pow(temp_ind) = 0;
    
end

data.dep_hat = exp(c.logME*data.logME + ...
    c.short_increase*data.short_increase + ...
    c.long_decrease*data.long_decrease + ...
    c.currencies_potential*data.currencies_potential + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for ccu=1:length(unique_curr)
        
        data.w_hat(temp_ind(ccu)) = data.dep_hat(temp_ind(ccu))./(1+sum(data.dep_hat(temp_ind)));
        
    end
end

data.mktcap_hat = data.w_hat.*data.wealth;

scalefactor_wave1 = zeros(length(unique_curr),1);
scalefactor_wave2 = zeros(length(unique_curr),1);
supply_wave1 = zeros(length(unique_curr),1);
supply_wave2 = zeros(length(unique_curr),1);

for ii=1:length(unique_curr)
    
    temp = (data.currency_id2==ii);
    
    mktcap_wave1_hat = nansum(data.mktcap_hat(temp&data.wave==1)); % there are NaNs her. WHY???
    mktcap_wave1 = nanmean(data.marketcap(temp&data.wave==1));
    scalefactor_wave1(ii) = mktcap_wave1/mktcap_wave1_hat;
    supply_wave1(ii) = nanmean(data.supply(temp&data.wave==1));
    
    mktcap_wave2_hat = sum(data.mktcap_hat(temp&data.wave==2));
    mktcap_wave2 = mean(data.marketcap(temp&data.wave==2));
    scalefactor_wave2(ii) = mktcap_wave2/mktcap_wave2_hat;
    supply_wave2(ii) = mean(data.supply(temp&data.wave==2));
    
end

%% compute equilibrium prices

fun1 = @(p) eq_p_fun(p,data,c,scalefactor_wave1,supply_wave1,1,ID_unique,unique_curr);
fun2 = @(p) eq_p_fun(p,data,c,scalefactor_wave2,supply_wave2,2,ID_unique,unique_curr);

p0_1 = zeros(length(unique_curr),1);
for ii=1:length(unique_curr)
    temp = (data.currency_id2==ii);
    p0_1(ii) = log(mean(data.open(temp&data.wave==1)));
end
[p_eq_1] = solve_contraction(fun1,p0_1);

p0_2 = zeros(length(unique_curr),1);
for ii=1:length(unique_curr)
    temp = (data.currency_id2==ii);
    p0_2(ii) = log(mean(data.open(temp&data.wave==2)));
end
[p_eq_2] = solve_contraction(fun2,p0_2);

%% compute equilibrium allocations

mktcap_hat_1 = zeros(length(unique_curr),1);
mktcap_hat_2 = zeros(length(unique_curr),1);

for ii=1:length(unique_curr)
    
    temp = (data.currency_id2==ii);
    
    mktcap_hat_1(ii) = nanmedian(data.mktcap_hat(temp&data.wave==1));
    mktcap_hat_2(ii) = nanmedian(data.mktcap_hat(temp&data.wave==2));
        
end

cash_all = NaN(length(ID_unique),1);
wave_vec = NaN(length(ID_unique),1);

for ii=1:length(ID_unique)

    temp = (data.household_id==ID_unique(ii));
    
    temp_sum_w = nansum(data.w_hat(temp));

    wealth_temp = unique(data.wealth(temp));
    wave_vec(ii) = unique(data.wave(temp));

    cash_all(ii) = (1-temp_sum_w)*wealth_temp;
    
end

cash_1 = nanmedian(cash_all(wave_vec==1));
cash_2 = nanmedian(cash_all(wave_vec==2));

%% set all potential for pow crypto =0

NS = 150;

perc_change = 25/100;

p_eq_1_pow_neg_mat = NaN(size(unique_curr,1),NS);
p_eq_2_pow_neg_mat = NaN(size(unique_curr,1),NS);
mktcap_hat_1_pow_neg_mat = NaN(size(unique_curr,1),NS);
mktcap_hat_2_pow_neg_mat = NaN(size(unique_curr,1),NS);
cash_1_pow_neg_mat = NaN(1,NS);
cash_2_pow_neg_mat = NaN(1,NS);

for nn=1:NS
    
    data2 = data;
    
    % take people who are optimistic about AT LEAST ONE PoW cryptos
    temp_hh = unique(data2.household_id(data2.currency_id2~=1&data2.currency_id2~=8&data2.currencies_potential==1));
    rng(nn*123)
    temp_hh_change = temp_hh(rand(length(temp_hh),1)<=perc_change);
    for ii=1:length(temp_hh_change)
        
        temp_ind = find(data2.household_id==temp_hh_change(ii));
        
        temp_ind2 = temp_ind([2:7 9]);
        
        % make 25% of them not optimistic about ALL PoW cryptos
        data2.currencies_potential(temp_ind2) = 0;
        
    end
        
    fun1 = @(p) eq_p_fun(p,data2,c,scalefactor_wave1,supply_wave1,1,ID_unique,unique_curr);
    fun2 = @(p) eq_p_fun(p,data2,c,scalefactor_wave2,supply_wave2,2,ID_unique,unique_curr);
    
    p0_1 = zeros(length(unique_curr),1);
    for ii=1:length(unique_curr)
        temp = (data2.currency_id2==ii);
        p0_1(ii) = log(mean(data2.open(temp&data2.wave==1)));
    end
    [p_eq_1_pow_neg_mat(:,nn)] = solve_contraction(fun1,p0_1);
    
    p0_2 = zeros(length(unique_curr),1);
    for ii=1:length(unique_curr)
        temp = (data2.currency_id2==ii);
        p0_2(ii) = log(mean(data2.open(temp&data2.wave==2)));
    end
    [p_eq_2_pow_neg_mat(:,nn)] = solve_contraction(fun2,p0_2);
    
    %% allocation
    
    p_eq_pow_neg_vec = NaN(size(data2,1),1);
    for ii=1:length(unique_curr)
        temp1 = (data2.currency_id2==ii&data2.wave==1);
        p_eq_pow_neg_vec(temp1) = exp(p_eq_1_pow_neg_mat(ii,nn));
        temp2 = (data2.currency_id2==ii&data2.wave==2);
        p_eq_pow_neg_vec(temp2) = exp(p_eq_2_pow_neg_mat(ii,nn));
    end
    
    ME_pow_neg = data2.supply.*p_eq_pow_neg_vec;
    
    dep_hat_pow_neg = exp(c.logME*log(ME_pow_neg) + ...
    c.short_increase*data2.short_increase + ...
    c.long_decrease*data2.long_decrease + ...
    c.currencies_potential*data2.currencies_potential + ...
    c.incomenewdummy*data2.incomenewdummy + c.agenewdummy*data2.agenewdummy + ...
    c.countrynewdummy*data2.countrynewdummy + ...
    c.type_objective*data2.type_objective +...
    c.log_libor*data2.log_libor + c.log_sp500*data2.log_sp500 + c.cons +...
    c.pow*data2.pow +...
    c.beta*data2.beta + c.momentum4*data2.momentum4);
    
    w_hat_pow_neg = zeros(size(data2,1),1);
    
    for ii=1:length(ID_unique)
        
        temp_ind = find(data2.household_id==ID_unique(ii));
        
        for ccu=1:length(unique_curr)
            
            w_hat_pow_neg(temp_ind(ccu)) = dep_hat_pow_neg(temp_ind(ccu))./(1+nansum(dep_hat_pow_neg(temp_ind)));
            
        end
    end
    
    mktcap_hat_pow_neg_all = w_hat_pow_neg.*data2.wealth;
    
    for ii=1:length(unique_curr)
        
        temp = (data2.currency_id2==ii);
        
        mktcap_hat_1_pow_neg_mat(ii,nn) = nanmedian(mktcap_hat_pow_neg_all(temp&data2.wave==1));
        mktcap_hat_2_pow_neg_mat(ii,nn) = nanmedian(mktcap_hat_pow_neg_all(temp&data2.wave==2));
        
    end
    
    % cash
    
    cash_pow_neg_all = NaN(length(ID_unique),1);
    wave_vec = NaN(length(ID_unique),1);
    
    for ii=1:length(ID_unique)
        
        temp = (data2.household_id==ID_unique(ii));
        
        temp_sum_w = nansum(w_hat_pow_neg(temp));
        
        wealth_temp = unique(data2.wealth(temp));
        wave_vec(ii) = unique(data2.wave(temp));
        
        cash_pow_neg_all(ii) = (1-temp_sum_w)*wealth_temp;
        
    end
    
    cash_1_pow_neg_mat(nn) = nanmedian(cash_pow_neg_all(wave_vec==1));
    cash_2_pow_neg_mat(nn) = nanmedian(cash_pow_neg_all(wave_vec==2));
    nn
end

%% collect results

r.p_eq_baseline = exp(p_eq_1);
r.p_eq_green = mean(exp(p_eq_1_pow_neg_mat),2);


r.allocations_baseline = mktcap_hat_1;
r.allocations_green = mean(mktcap_hat_1_pow_neg_mat,2);

r = struct2table(r);

writetable(r,'../../output/tables/table8_green.xlsx')

cc.cash_baseline = cash_1;
cc.cash_green = mean(cash_1_pow_neg_mat,2);

cc = struct2table(cc);

writetable(cc,'../../output/tables/table8_green_cash.xlsx')
